﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eGroup : EditBase
    {

        #region Public Members

        public bool IsNewItem
        {
            get { return (ActiveItemId == Guid.Empty); }
        }


        public Guid ActiveItemId
        {
            get { return (hfItemId.Value != string.Empty) ? new Guid(hfItemId.Value) : Guid.Empty; }
            set { hfItemId.Value = value.ToString(); }
        }

        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (ItemId != Guid.Empty)
                ActiveItemId = ItemId;

            if (ActiveItemId != Guid.Empty)
            {
                bindGroup();
            }
            else
            {
                bindConfigurationList(new Guid());
            }

           
        }

        #endregion

        #region Helpers

        private void bindGroup()
        {
            
            GroupController gc = new GroupController();
            GroupInfo gi = gc.SelectGroup(ActiveItemId);
            if (gi != null)
            {
                lblHeaderRight.Text = gi.Name;
                txtName.Text = gi.Name;
                bindConfigurationList(gi.ConfigurationId);
            }


        }

        private void bindConfigurationList(Guid id)
        {
            // Configurations
            ConfigurationController cc = new ConfigurationController();
            List<ConfigurationInfo> ciList = cc.SelectConfigurations("Name Asc");
            ddConfiguration.DataSource = ciList;
            ddConfiguration.DataTextField = "Name";
            ddConfiguration.DataValueField = "ConfigurationId";
            ddConfiguration.DataBind();
            ddConfiguration.Items.Insert(0, new RadComboBoxItem("<None>", Guid.Empty.ToString()));
            if (id != Guid.Empty)
                ddConfiguration.Items.FindItemByValue(id.ToString()).Selected = true;

        }

        #endregion


        #region Action Event Helpers


        private void SaveItem()
        {
            if (Page.IsValid)
            {
                GroupController gc = new GroupController();
                GroupInfo gi = new GroupInfo();

                if (!IsNewItem)
                    gi = gc.SelectGroup(ActiveItemId);

                gi.Name = txtName.Text;
                gi.ConfigurationId = new Guid(ddConfiguration.SelectedValue);

                if (IsNewItem)
                    gc.InsertGroup(gi);
                else
                    gc.UpdateGroup(gi);

                RaiseNavEvent(this, new NavEventArgs("eGroups", ActiveItemId));
            }


        }


        private void DeleteItem()
        {
            try
            {
                GroupController gc = new GroupController();
                gc.DeleteGroup(ActiveItemId);
            }
            catch { }
            RaiseNavEvent(this, new NavEventArgs("eGroups", ActiveItemId));
        }


        #endregion


        #region EditBase
        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Save":
                    SaveItem();
                    break;
                case "Delete":
                    DeleteItem();
                    break;
                case "Return":
                    RaiseNavEvent(this, new NavEventArgs("eGroups", ActiveItemId));
                    break;
                default:
                    break;
            }
           
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Save", "Save Changes", "~/images/icons/save.png", "", true, true));
                al.Add(new ActionInfo("Delete", "Delete Group", "~/images/icons/delete.png", "Are you sure you want to delete this user", !IsNewItem));
                al.Add(new ActionInfo("Return", "Return", "~/images/icons/undo.png"));
                return al;

            }
        }

        #endregion
    }
}